#ifndef __GAME_H__
#define __GAME_H__

/**
 * 存放所有字模数据
 */
// unsigned char code START_PIC[] = {
//     0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0xCF, 0xCF, 0xCF, 0xCF, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x00, 0x00, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F,
//     0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x00, 0x00, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0xCF, 0xCF, 0xCF, 0xCF, 0x0F, 0x0F,
//     0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x3F, 0x3F, 0xFC, 0xFC, 0xFC, 0xFC,
//     0xC0, 0xC0, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x0F, 0x0F, 0x0F, 0x0F, 0xCF, 0xCF, 0xCF, 0xCF, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x07, 0x07,
//     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xF0, 0xF0,
//     0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00,
//     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0F, 0x0F, 0x0F, 0x0F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0xC0, 0xF0, 0xF0, 0xFF, 0xFF,
//     0x0F, 0x0F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x03, 0x0F, 0x0F, 0x3F, 0x3F, 0xFC, 0xFC, 0xF0, 0xF0, 0xC0, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
//     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00,
//     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00,
//     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x3F, 0x3F, 0xFF, 0xFF, 0xC3, 0xC3, 0x00, 0x00,
//     0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x03, 0x0F, 0x0F, 0x3F, 0x3F, 0xFC, 0xFC, 0xF0, 0xF0, 0xC0, 0x80,
//     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C, 0x0F, 0x0F, 0x0F, 0x0F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
//     0x00, 0x00, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0F, 0x0F, 0x0F, 0x0F, 0x00, 0x00,
//     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C, 0x0F, 0x0F, 0x0F, 0x0F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x07, 0x0F, 0x0F, 0x0F, 0x0F,
//     0x0C, 0x08, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x03, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0F, 0x0F, 0x0F, 0x0F};

code unsigned char START_PIC[] = {
    // "袁"字（索引0）
    0x10, 0x10, 0x14, 0xD4, 0x54, 0x54, 0x54, 0x5F, 0x54, 0x54, 0x54, 0xD4, 0x14, 0x10, 0x10, 0x00, // 上半部分
    0x20, 0x20, 0x10, 0x13, 0xFA, 0x46, 0x22, 0x06, 0x0A, 0x12, 0x22, 0x53, 0x48, 0x84, 0x80, 0x00, // 下半部分

    // "宇"字（索引1）
    0x10, 0x0C, 0x24, 0x24, 0x24, 0x24, 0x25, 0xE6, 0x24, 0x24, 0x24, 0x24, 0x24, 0x14, 0x0C, 0x00, // 上半部分
    0x02, 0x02, 0x02, 0x02, 0x02, 0x42, 0x82, 0x7F, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x00, // 下半部分

    // "璐"字（索引2）
    0x84, 0x84, 0xFC, 0x84, 0x84, 0x3E, 0x22, 0xE2, 0x3E, 0x10, 0xAC, 0x47, 0xA4, 0x1C, 0x00, 0x00, // 上半部分
    0x10, 0x30, 0x1F, 0x08, 0x48, 0x7E, 0x40, 0x3F, 0x22, 0x03, 0xFC, 0x44, 0x44, 0xFD, 0x01, 0x00  // 下半部分

};

// 数字字模，4x8
unsigned char code NUM_CHARS[] = {
    0x7F, 0x41, 0x41, 0x7F, // 0字模
    0x00, 0x00, 0x00, 0x7F, // 1字模
    0x79, 0x49, 0x49, 0x4F, // 2字模
    0x49, 0x49, 0x49, 0x7F, // 3字模
    0x0F, 0x08, 0x08, 0x7F, // 4字模
    0x4F, 0x49, 0x49, 0x79, // 5字模
    0x7F, 0x49, 0x49, 0x79, // 6字模
    0x01, 0x01, 0x01, 0x7F, // 7字模
    0x7F, 0x49, 0x49, 0x7F, // 8字模
    0x4F, 0x49, 0x49, 0x7F, // 9字模
};

/**
 * 左右箭头，4x8
 */
unsigned char code LEFT_ARROW[] = {0x08, 0x1c, 0x3e, 0x7f};
unsigned char code RIGHT_ARROW[] = {0x7f, 0x3e, 0x1c, 0x08};

/**
 * SPEED 字模，4x8
 */
unsigned char code SPEED_CHARS[] = {
    0x46, 0x49, 0x49, 0x31,  // S
    0x00,                    // 空一列
    0x7F, 0x09, 0x09, 0x06,  // P
    0x00,                    // 空一列
    0x7F, 0x49, 0x49, 0x49,  // E
    0x00,                    // 空一列
    0x7F, 0x49, 0x49, 0x49,  // E
    0x00,                    // 空一列
    0x7F, 0x41, 0x41, 0x3E}; // D
// score字模
unsigned char code SCORE_CHARS[] = {0x46, 0x49, 0x49, 0x31, 0x00, 0x3E, 0x41, 0x41, 0x22, 0x00, 0x3E, 0x41,
                                    0x41, 0x3E, 0x00, 0x7F, 0x11, 0x29, 0x46, 0x00, 0x7F, 0x49, 0x49, 0x49};

unsigned char code RIGHT_WORDS_CN[] = {
    0x00, 0xFE, 0x02, 0x22, 0x42, 0x82, 0x72, 0x02, 0x22, 0x42, 0x82, 0x72, 0x02, 0xFE, 0x00, 0x00,
    0x00, 0xFF, 0x10, 0x08, 0x06, 0x01, 0x0E, 0x10, 0x08, 0x06, 0x01, 0x4E, 0x80, 0x7F, 0x00, 0x00, /*"网",0*/
    0x20, 0x20, 0xFF, 0x20, 0x24, 0xF4, 0x54, 0x54, 0x54, 0xFF, 0x54, 0x55, 0x56, 0xF4, 0x04, 0x00,
    0x00, 0x00, 0xFF, 0x00, 0x08, 0x0B, 0x19, 0x69, 0x09, 0x0B, 0x49, 0x89, 0x7D, 0x0B, 0x08, 0x00}; /*"博",1*/

unsigned char code RIGHT_WORDS_EN[] = {
    0x00, 0x00, 0x02, 0xFE, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x3F, 0x20, 0x00, 0x00, 0x00, /*"I",0*/
    0x02, 0x02, 0x02, 0xFE, 0x02, 0x02, 0x02, 0x00, 0x00, 0x00, 0x00, 0x3F, 0x00, 0x00, 0x00, 0x00, /*"T",1*/
    0xF8, 0x04, 0xE2, 0x12, 0xF2, 0x04, 0xF8, 0x00, 0x0F, 0x10, 0x23, 0x24, 0x27, 0x24, 0x13, 0x00, /*"@",2*/
    0x00, 0x80, 0x78, 0x06, 0x78, 0x80, 0x00, 0x00, 0x38, 0x07, 0x04, 0x04, 0x04, 0x07, 0x38, 0x00, /*"A",3*/
    0xFE, 0x18, 0x60, 0x80, 0x00, 0x00, 0xFE, 0x00, 0x3F, 0x00, 0x00, 0x01, 0x06, 0x18, 0x3F, 0x00, /*"N",4*/
    0x06, 0x18, 0x60, 0x80, 0x60, 0x18, 0x06, 0x00, 0x00, 0x00, 0x00, 0x3F, 0x00, 0x00, 0x00, 0x00  /*"Y",5*/
};

unsigned char code PAUSE_CHARS[] = {
    0x60, 0xE0, 0xE0, 0x60, 0x60, 0x60, 0xE0, 0xC0, 0xC0, 0xFF, 0xFF, 0xD8, 0x18, 0x18, 0x1F, 0x0F, /*"p",0*/
    0x00, 0xC0, 0xE0, 0x60, 0x60, 0xE0, 0xC0, 0x00, 0x00, 0x0E, 0x1E, 0x1B, 0x19, 0x1F, 0x1F, 0x18, /*"a",1*/
    0x60, 0xE0, 0xE0, 0x00, 0x00, 0x60, 0xE0, 0xE0, 0x00, 0x0F, 0x1F, 0x18, 0x18, 0x18, 0x1F, 0x1F, /*"u",2*/
    0x00, 0xC0, 0xE0, 0x60, 0x60, 0xE0, 0xE0, 0x00, 0x00, 0x1C, 0x1D, 0x19, 0x1B, 0x1E, 0x0E, 0x00, /*"s",3*/
    0x00, 0xC0, 0xE0, 0x60, 0x60, 0xE0, 0xC0, 0x00, 0x00, 0x0F, 0x1F, 0x19, 0x19, 0x1D, 0x0D, 0x00  /*"e",4*/
};

/**
 * 使用一个2byte数据描述一个方块形状，对应一个4x4，一共16个格子区域
 * 换算到显示的像素，每个格子对应4x4像素
 */
unsigned int code TETRIS_SHAPES[7][4] = {
    // I
    {0xF000, 0x4444, 0xF000, 0x4444},
    // L
    {0xE800, 0xc440, 0x2e00, 0x88c0},
    // J
    {0xE200, 0x44c0, 0x8e00, 0xc880},
    // O
    {0xcc00, 0xcc00, 0xcc00, 0xcc00},
    // Z
    {0xc600, 0x4c80, 0xc600, 0x4c80},
    // S
    {0x6c00, 0x8c40, 0x6c00, 0x8c40},
    // T
    {0x4e00, 0x8c80, 0xe400, 0x4c40}};

// 十六个元素，每个代表一行，每行10列，
// 0010 0000 0000 0100 ,中间10位0，每位代表一个4x4像素的格子
unsigned int code GAME_ZONE_INIT_DATA[] = {
    0x2004, 0x2004, 0x2004, 0x2004,
    0x2004, 0x2004, 0x2004, 0x2004, //
    0x2004, 0x2004, 0x2004, 0x2004,
    0x2004, 0x2004, 0x2004, 0x2004,
    // 多出来3行用来防止数组越界，保持处理逻辑一致
    // 形状是4x4方格，最左上角是坐标原点，允许坐标落到游戏区域的最底端，这时候向下伸出了3行
    0xFFFF, 0xFFFF, 0xFFFF};

unsigned char code GAME_OVER_CHARS[] = {
    0x00, 0xF0, 0xF8, 0x08, 0x08, 0x38, 0x30, 0x00, 0x00, 0x07, 0x0F, 0x08, 0x09, 0x0F, 0x0F, 0x00, /*"G",0*/
    0x00, 0xE0, 0xF0, 0x18, 0x18, 0xF0, 0xE0, 0x00, 0x00, 0x0F, 0x0F, 0x01, 0x01, 0x0F, 0x0F, 0x00, /*"A",1*/
    0x00, 0xF8, 0xF8, 0x20, 0xC0, 0x20, 0xF8, 0xF8, 0x00, 0x0F, 0x0F, 0x00, 0x01, 0x00, 0x0F, 0x0F, /*"M",2*/
    0x00, 0xF8, 0xF8, 0x88, 0x88, 0x88, 0x08, 0x00, 0x00, 0x0F, 0x0F, 0x08, 0x08, 0x08, 0x08, 0x00, /*"E",3*/
    0x00, 0xF0, 0xF8, 0x08, 0x08, 0xF8, 0xF0, 0x00, 0x00, 0x07, 0x0F, 0x08, 0x08, 0x0F, 0x07, 0x00, /*"O",4*/
    0x00, 0xF8, 0xF8, 0x00, 0x00, 0xF8, 0xF8, 0x00, 0x00, 0x03, 0x07, 0x0C, 0x0C, 0x07, 0x03, 0x00, /*"V",5*/
    0x00, 0xF8, 0xF8, 0x88, 0x88, 0x88, 0x08, 0x00, 0x00, 0x0F, 0x0F, 0x08, 0x08, 0x08, 0x08, 0x00, /*"E",6*/
    0x00, 0xF8, 0xF8, 0x88, 0x88, 0xF8, 0x70, 0x00, 0x00, 0x0F, 0x0F, 0x00, 0x01, 0x0F, 0x0E, 0x00, /*"R",7*/
};

unsigned int idata GAME_ZONE[19];

#define GAME_FRAME_TOTAL 10
unsigned char game_frame_count = 0;

struct
{
    // TETRIS_SHAPES二维数组的两个维度
    unsigned char shape_type; // 方块的形状类型 0-6
    unsigned char shape_pos;  // 方块的方位 0-3
    // 位于游戏区域的坐标
    unsigned char x;         // 水平方位 0~9
    unsigned char y;         // 竖直的方位  0~15
    unsigned int shape;      // 形状数据 TETRIS_SHAPES中的一条数据
    unsigned char next_type; // 下一个方块的类型
    unsigned char next_pos;  // 下一个方块的方位
} game_shape;

sbit BTN_START = P3 ^ 3;
sbit BTN_PAUSE = P3 ^ 5;
sbit BTN_UP = P1 ^ 4;
sbit BTN_DOWN = P1 ^ 6;
sbit BTN_LEFT = P1 ^ 0;
sbit BTN_RIGHT = P1 ^ 2;

#define PRESSED_NONE 0
#define PRESSED_START 1
#define PRESSED_PAUSE 2
#define PRESSED_UP 3
#define PRESSED_DOWN 4
#define PRESSED_LEFT 5
#define PRESSED_RIGHT 6

#define STATUS_START 0
#define STATUS_SPEED_SELECT 1
#define STATUS_PALYING 2
#define STATUS_GAME_OVER 3
#define STATUS_PAUSE 4
unsigned char game_status = STATUS_START;
unsigned char game_speed = 0;
unsigned int game_score = 0;
unsigned int game_eliminated_lines = 0;

#define BTN_COUNT_DOWN 50
// 做持续按下的计数
unsigned int game_button_pressed_count = 0;
// 上一次按下的按键
unsigned int game_button_last_pressed = PRESSED_NONE;

#define BUTTON_ACCELERATION_MAX 4
// 连续按下按键，等待之后速度加速
unsigned char game_button_acceleration = 1;

#endif